{% extends "wiki/base.html" %}
{% from "layout/errorlist.html" import errorlist %}
{% from "wiki/includes/sidebar_modules.html" import document_tools with context %}
{% from "includes/common_macros.html" import content_editor with context %}
{% from "wiki/includes/document_macros.html" import revision_diff, submit_revision, edit_messages with context %}
{% from "wiki/includes/document_macros.html" import document_lock_warning with context %}
{% set title = _('Translate Article | {document}')|f(document=parent.title) %}
{% set crumbs = [(parent.get_absolute_url(), parent.title),
                 (None, _('Translate Article'))] %}
{% set classes = 'translate' %}
{% set styles = ('wiki', 'wiki-editor', 'jqueryui') %}
{% set scripts = ('wiki', 'wiki.diff', 'wiki.editor', 'jqueryui') %}

{% block content %}
  {% set language = settings.LANGUAGES_DICT[locale.lower()] %}
  <div>
    {{ document_tools(document, parent, user, 'edit', settings) }}

    <article id="localize-document" class="main form-page{% if locked %} locked{% endif %}" data-slug="{{ parent.slug }}">

      <ul class="user-messages">
        {% if fxos_l10n_warning %}
          <li class="info dismissible" id="fxos-l10n-warning">
            <p>
              {% trans %}
                Thanks for your help! Just so you know, Firefox OS is
                not available in your locale yet, and it's possible this
                article will change before it is.
              {% endtrans %}
            </p>

            <p>
              {% trans email_link='<a href="mailto:mana@mozilla.com">mana@mozilla.com</a>'|safe %}
                If you have any questions about localization priorities,
                please contact {{ email_link }}.
              {% endtrans %}
            </p>

            <button class="btn dismiss">{{ _("I understand, don't show this again.") }}</button>
          </li>
        {% endif %}

        {% if not based_on.is_ready_for_localization %}
          <li class="warning">
            {{ _('You are translating an English document which is not yet ready for localization.') }}
          </li>
        {% endif %}

        {% if draft_revision %}
          <li class="info">
            <p><strong>{{ _('You have a draft revision for this article saved on {date_time}')|f(date_time=draft_revision.created) }}</strong></p>
            <form action="" method="get">
              <input class="btn" name="restore" value={{ _('Restore') }} type="submit">
              <input class="btn" name="discard" value={{ _('Discard') }} type="submit">
            </form>
          </li>
        {% endif %}
        {% if more_updated_rev %}
          <li class="warning draft-warning">
            <p><strong>{{ _('This version is outdated, but there is a new version <a href="{link}">{revision}</a> available.')|fe(link=url('wiki.revision', document.slug, more_updated_rev.id, locale=locale), revision=more_updated_rev.id) }}</strong></p>
          </li>
        {% endif %}
        {{ edit_messages(document, show_revision_warning) }}
        {{ document_lock_warning() }}
      </ul>

      <h1>{{ _('Translating {title}')|f(title=parent.title) }}</h1>
      <div class="change-locale">
        <p>
          {{ _('Translating article to {locale}.' )|f(locale=language) }}
        </p>
      </div>
      {% if not document %}
        {# If this is the first translation to this locale, we use 1 big form. #}
        <form id="both_form" action="" method="post" data-json-url="{{ url('wiki.json') }}">
          {% csrf_token %}
          <input type="hidden" name="form" value="both" />
          <input type="hidden" name="slug" value="{{ parent.slug }}" />
          <input type="hidden" name="locale" value="{{ parent.locale }}">
      {% endif %}
      {% if document_form %}
        <details class="h2"{% if disclose_description %} open="open"{% endif %}>
          <summary>{{ _('Translate Description') }}</summary>
          {{ errorlist(document_form) }}
          {% if document %}
            {# If there are existing translations in this locale, we use 2 separate forms. #}
            <form id="doc_form" action="" method="post" data-json-url="{{ url('wiki.json') }}" data-document-id="{{ document.id }}">
              {% csrf_token %}
              <input type="hidden" name="form" value="doc" />
              <input type="hidden" name="slug" value="{{ document.slug }}" />
              <input type="hidden" name="locale" value="{{ document.locale }}">
          {% endif %}
          <ul class="description">
            <li>
              <div class="approved">
                <h3>{{ _('Title:') }}</h3>
                <div class="val">{{ parent.title }}</div>
              </div>
              <div class="localized">
                <h3><label for="{{ document_form.title.auto_id }}" title="{{ document_form.title.help_text }}">{{ _('Title in {locale}:')|f(locale=language) }}</label></h3>
                {{ document_form.title|safe }}
              </div>
            </li>
            <li>
              <div class="approved">
                <h3>{{ _('Slug:') }}</h3>
                <div class="val">{{ parent.slug }}</div>
              </div>
              <div class="localized">
                <h3><label for="{{ document_form.slug.auto_id }}" title="{{ document_form.slug.help_text }}">{{ _('Slug in {locale}:')|f(locale=language) }}</label></h3>
                {{ document_form.slug|safe }}
              </div>
            </li>
            <li>
              {{ document_form.allow_discussion|label_with_help }}{{ document_form.allow_discussion }}
            </li>
          </ul>
          {% if document %}
              <div class="submit">
                <button class="btn btn-important" type="submit">{{ _('Save description') }}</button>
              </div>
            </form>
          {% endif %}
        </details>
      {% endif %}

      {% if revision_form %}
        <details class="h2" open="open">
          <summary>{{ _('Translate Content') }}</summary>
          {{ errorlist(revision_form) }}
          {% if document %}
            {# If there are existing translations in this locale, we use 2 separate forms. #}
            <form id="rev_form" action="" method="post">
              {% csrf_token %}
              <input type="hidden" name="form" value="rev" />
              <input type="hidden" name="slug" value="{{ document.slug }}" />
              <input type="hidden" name="locale" value="{{ document.locale }}" />
              {# Do not add a closing form tag here it is added below #}
          {% endif %}
          {% if recent_approved_revs %}
            <h3>{{ _('Recently approved updates') }}:</h3>
            <ul class="recent-revisions">
              {% for rev in recent_approved_revs %}
                <li>
                  <a href="{{ url('wiki.revision', parent.slug, rev.id, locale=parent.locale) }}" rel="nofollow, noindex">{{ datetimeformat(rev.created, format='date') }}</a>:
                  [<a href="{{ profile_url(rev.creator) }}">{{ display_name(rev.creator) }}</a>]
                  {{ rev.comment }}
                </li>
              {% endfor %}
            </ul>
          {% endif %}

          <h3>{{ _('Differences') }}:</h3>

          {% if document.current_revision and document.current_revision.based_on and parent.latest_localizable_revision %}
            {# Diff between the English version the current translation is based on and the most recent ready-to-localize English version. #}
            {{ revision_diff(document.current_revision.based_on, parent.latest_localizable_revision, show_picker=True) }}
          {% elif not parent.latest_localizable_revision %}
            <p>{{ _('Note: This article has no revisions ready for localization.') }}</p>
          {% else %}
            <p>{{ _('Note: There are no approved translations for this article.') }}</p>
          {% endif %}
          <ul class="description">
            <li>
              <div class="approved">
                <h3><label>{{ _('Keywords:') }}</label></h3>
                <div class="val">{{ based_on.keywords }}</div>
              </div>
              <div class="localized">
                <h3><label for="{{ revision_form.keywords.auto_id }}" title="{{ revision_form.keywords.help_text }}">{{ _('Keywords in {locale}:')|f(locale=language) }}</label></h3>
                {{ revision_form.keywords|safe }}
              </div>
            </li>
            <li>
              <div class="approved">
                <h3><label>{{ _('Search result summary:') }}</label></h3>
                <div class="val">{{ based_on.summary }}</div>
              </div>
              <div class="localized">
                <h3><label for="{{ revision_form.summary.auto_id }}" title="{{ revision_form.summary.help_text }}">{{ _('Search result summary in {locale}:')|f(locale=language) }}</label></h3>
                {{ revision_form.summary|safe }}
                <p><span id="remaining-characters">{{ revision_form.summary.field.max_length }}</span> {{_('characters remaining.') }}</p>
              </div>
            </li>
          </ul>
          <div id="content-fields">
            <div class="approved">
              <h3>{{ _('Approved {default_locale} version:')|f(default_locale=settings.LANGUAGES_DICT[settings.WIKI_DEFAULT_LANGUAGE.lower()]) }}</h3>
              <div id="content-or-diff" class="content">
                <textarea readonly="readonly"{% if settings.WIKI_DEFAULT_LANGUAGE.lower() not in settings.LANGUAGES_BIDI %} dir="ltr"{% endif %}>{{ based_on.content }}</textarea>
              </div>
            </div>
            <div class="localized">
              <h3>{{ _('{locale} translation:')|f(locale=language) }}</h3>
              {{ content_editor(revision_form.content) }}
            </div>
          </div>
          {{ revision_form.hidden_fields()|join|safe }}
          <div class="buttons-and-preview">
            {# If the document has been created and has a current revision,
               we allow the localizer to keep the translation out of date
               with this new revision. #}
            {{ submit_revision(revision_form, show_no_update_checkbox=(document and document.current_revision), include_diff=True, translate=True) }}
            <div id="preview" class="cf"></div>
            <div id="preview-diff">
                <div class="from">{{ revision_form.content.value() }}</div>
                <div class="to"></div>
                <div class="output"></div>
              </div>
            <div class="submit" id="preview-bottom">
              {{ submit_revision(revision_form, buttons_only=True, include_diff=True, translate=True) }}
            </div>
            <div id="draft-message" class="alert" hidden>
            </div>
          </div>
          {% if document %}
            </form>
          {% endif %}
        </details>
      {% endif %}
      {% if not document %}
        </form>
      {% endif %}
    </article>
  </div>

  <script type="application/json" class="showfor-data">
    {{ showfor_data(parent.get_products()) }}
  </script>
{% endblock %}

{% block side_promos %}{% endblock %}
